<!DOCTYPE html>
<html>

<head>
    <title>编辑游记</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap/css/bootstrap.min.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/editTravels.css"/>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css"/>
    <script src="/js/plugins/dialog2/dialog.min.js"></script>
    <script src="../js/plugins/jquery-form/jquery.form.js"></script>
    <script src="/js/plugins/eleditor/Eleditor.min.js"></script>
    <script src="/js/plugins/eleditor/webuploader.min.js"></script>
    <script src="/js/plugins/My97DatePicker/WdatePicker.js"></script>
    <script src="/js/common.js"></script>
    <script>
        $(function () {

            //地区下拉框--------------------------------------------------
            $.get("/regions",function (data) {
                var op = "";
                $.each(data,function (index, item) {
                    op += "<option value='"+ item.id +"'>"+ item.name +"</option>"
                });
                $("#regions").html(op);
                //获取id------------------------------------------------
                //引入文件common.js中的方法,表示获得当前路径的?号之后的数据,自动包装成数组
                var params = getParams();
                var id = params.id;
                if (id){
                    //获取指定id的游记
                    $.get("/travels/"+ id, function (data) {
                        console.log(data);
                        //回显
                        $(".travels").renderValues(data);

                        $("input[name='_method']").val("put")
                    })
                }
                //end------------------------------------------------

            });

            //end--------------------------------------------------------


            //富文本编辑器------------------------------------------------
            var Edr = new Eleditor({
                el:'#contentEditor',//将编辑器放在哪里
                upload:{
                    server:'/images',
                },
            });
            //end--------------------------------------------------------

            //保存--------------------------------------------------------
            //提交表单之前需要额外添加结果参数
            var state = $(this).data("state");
            $("#save").click(function () {
                //因为下面的四个参数不在上面的form表单中,所以需要手动添加
                $("#detailForm").ajaxSubmit({
                    //表示表单提交前先执行该方法
                    //params:一个数组,表示form表单空间值:[{name:属性名1, value:属性1},{name:属性名2, value:属性2}]
                    //form:要提交的form表单
                    //options: form表单与ajaxForm插件相关信息
                    beforeSubmit:function (params, form, options) {
                        //状态
                        params.push({name:"state",value:state});
                        //封面
                        params.push({name:"coverUrl",value:$("#coverUrl").attr("src")});
                        //标题
                        params.push({name:"title",value:$("#title").val()});
                        //内容,富文本编辑器中输入的内容
                        params.push({name:"travelContent.content",value:Edr.getContent()});

                        //return false; 表示终止提交表单
                    },
                    success:function (data) { //表单提交成功

                        if(data.success){
                            window.location.href = "/mine/travels.html"
                        }
                    }
                });
            });
            //end--------------------------------------------------------



            //背景墙-----------------------------------------------------
            $("#changeBtn").click(function () {
                $("#imageFile").click();
            });
            $("#imageFile").change(function () {
                if(this.value){
                    $("#imageForm").ajaxSubmit(function (data) {
                        if(data.status==1){
                            //文件上传成功,回显图片
                            $("#coverUrl").attr("src",data.url);
                        }
                    })
                }
            });
            //end-----------------------------------------------------

        });
    </script>



</head>

<body>
<div class="search-head">
    <div class="row nav-search">
        <div class="col-2">
            <a href="javascript:window.history.go(-1);">
                <span><i class="fa fa-times"></i></span>
            </a>
        </div>
        <div class="col-5"></div>
        <div class="col" style="text-align: right;">
            <i class="fa fa-floppy-o fg-fw" id="save"><span>保存</span></i>
            <i class="fa fa-check" id="release"><span>发布</span></i>
        </div>
    </div>
</div>
<!--图片上传表单,enctype表示表单类型-->
<form action="/images" method="post" enctype="multipart/form-data" id="imageForm">
    <!--accept表示想要请求的类型时image图片类型display:none隐藏form表单-->
    <input type="file" name="file" accept="image/*" style="display: none" id="imageFile">
</form>
<div class="travels form-group">
    <div class="cover">
        <img id="coverUrl" width="100%" height="200px" render-src="coverUrl"/>
        <form id="coverForm" method="post" enctype="multipart/form-data">
            <input type="file" name="pic" style="display: none" accept="image/gif,image/jpeg,image/jpg,image/png"/>
            <button type="button" class="btn" id="changeBtn">
                <i class="fa fa-pencil-square-o"></i>
                <small>更换封面</small>
            </button>
        </form>

    </div>
    <div class="title">
        <input class="form-control" id="title" placeholder="请输入标题" render-value="title"/>
    </div>
    <h6 href="#">点击文章内容即可编辑</h6>
    <div id="contentEditor" render-html="travelContent.content">
    </div>
    <hr/>
    <div class="container">
        <form id="detailForm" method="post" action="/travels">

            <input type="hidden" name="id" render-value="id"/>
            <input type="hidden" name="_method" value=""/>

            <label>出行时间:</label>
            <input type="text" name="travelTime" class="form-control" onclick="WdatePicker()" render-value="travelTime" readonly>
            <label>出行天数:</label>
            <input type="number" name="days" class="form-control" render-value="days">
            <label>参与人物:</label>
            <select class="c-select form-control" name="person" render-value="person">
                <option value="1" selected>一个人的旅行</option>
                <option value="2">和父母</option>
                <option value="3">和朋友</option>
                <option value="4">和同事</option>
                <option value="5">和爱人</option>
                <option value="6">和其他</option>
            </select>
            <label>人均费用:</label>
            <input type="text" name="perExpends" class="form-control" render-value="perExpends">

            <label>旅游地点:</label>
            <select class="form-control" id="regions" name="place.id" render-value="regions.id">

            </select>

            <div class="form-check">
                <label class="form-check-label">
                    <input class="form-check-input" type="checkbox" name="isPublic"
                    >是否公开
                </label>
            </div>

        </form>
    </div>

    <hr/>

</div>


</body>

</html>